package xhwl.insurance.ui;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import xhwl.insurance.BaseFragmentActivity;
import xhwl.insurance.R;
import xhwl.insurance.adapter.FragmentAdapter;
import xhwl.insurance.ui.fragment.AllCardDealFragment;
import xhwl.insurance.util.DimenUtil;

public class CardDealListActivity extends BaseFragmentActivity implements View.OnClickListener {
    public static final String EXTRA_TAB_INDEX="tabIndex";
    ImageView backImg;
    TextView titleTxt;
    View indicator;
    TextView all, dealing, success, defeat,fail;
    ViewPager myViewpager;
    private ArrayList<Fragment> fragments;
    public static CardDealListActivity instance;
    int tabIndex;
    private RelativeLayout title_layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
        tabIndex=getIntent().getIntExtra(EXTRA_TAB_INDEX,0);
        setContentView(R.layout.activity_card_deal_list);
        instance = this;

    }

    @Override
    public void onResume() {
        super.onResume();
        setImmerseLayout(title_layout);
    }

    @Override
    public void initViews() {
        backImg = (ImageView) findViewById(R.id.title_left_imageview);
        titleTxt = (TextView) findViewById(R.id.title_textview);
        titleTxt.setText("HMO售卖明细");
        indicator = (View) findViewById(R.id.indicator);
        all = (TextView) findViewById(R.id.card_all);
        dealing = (TextView) findViewById(R.id.card_dealing);
        success = (TextView) findViewById(R.id.card_success);
        defeat = (TextView) findViewById(R.id.card_defeat);
        fail= (TextView) findViewById(R.id.card_fail);
        myViewpager = (ViewPager) findViewById(R.id.deal_list_viewpager);
        title_layout = (RelativeLayout) findViewById(R.id.title_layout);
        initData();
    }

    @Override
    public void initListener() {
        all.setOnClickListener(this);
        dealing.setOnClickListener(this);
        success.setOnClickListener(this);
        defeat.setOnClickListener(this);
        fail.setOnClickListener(this);
        backImg.setOnClickListener(this);
    }

    @Override
    public void loadData() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.card_all:
                myViewpager.setCurrentItem(0);
                break;
            case R.id.card_dealing:
                myViewpager.setCurrentItem(1);
                break;
            case R.id.card_success:
                myViewpager.setCurrentItem(2);
                break;
            case R.id.card_defeat:
                myViewpager.setCurrentItem(3);
                break;
            case R.id.card_fail:
                myViewpager.setCurrentItem(4);
                break;
            case R.id.title_left_imageview:
                finish();
                break;
        }
    }

    private void initData() {
        fragments = new ArrayList<Fragment>();
        //添加fragment
        //orderStatus中0-处理中，1-成功，3-无效 4-提交失败
        String[] orderStatus = new String[]{"", "0", "1", "3","4"};
        for (String status : orderStatus) {
            AllCardDealFragment order = new AllCardDealFragment();
            Bundle bundle = new Bundle();
            bundle.putString("orderStatus", status);
            order.setArguments(bundle);
            fragments.add(order);//全部订单
        }
        //1.给ViewPager填充Fragment
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        myViewpager.setAdapter(fragmentAdapter);
        updateTitle();
        //初始化指示线的宽度
        initIndicatorLine();
        intIndicator();
        myViewpager.setCurrentItem(tabIndex);
    }

    private void initIndicatorLine() {
        //1.计算线的宽度
        int width = getWindowManager().getDefaultDisplay().getWidth() / fragments.size();
        //2.将width设置给indicator
        ViewGroup.LayoutParams params = indicator.getLayoutParams();
        params.width = width;
        indicator.setLayoutParams(params);
    }

    private void intIndicator() {
        myViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /*
         * 当滑动ViewPager的时候会执行该方法
         * @param position 当前选中的位置
         * @param positionOffset 表示当前手指滑动距离的百分比
         * @param positionOffsetPixels 表示当前手指滑动的距离，单位是像素
         */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                //1.计算线需要滚动到的距离
                float targetX = indicator.getWidth() * position + positionOffsetPixels / fragments.size();
                //2.让线滚动到指定的距离
                ViewCompat.setTranslationX(indicator, targetX);
            }

            @Override
            public void onPageSelected(int position) {
                updateTitle();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updateTitle() {
        //1.获取当前ViewPager选择的页
        int currentItem = myViewpager.getCurrentItem();
        all.setSelected(currentItem == 0);
        dealing.setSelected(currentItem == 1);
        success.setSelected(currentItem == 2);
        defeat.setSelected(currentItem == 3);
        fail.setSelected(currentItem == 4);
        all.setTextColor(this.getResources().getColor(R.color.text_color_33));
        dealing.setTextColor(this.getResources().getColor(R.color.text_color_33));
        success.setTextColor(this.getResources().getColor(R.color.text_color_33));
        defeat.setTextColor(this.getResources().getColor(R.color.text_color_33));
        fail.setTextColor(this.getResources().getColor(R.color.text_color_33));
        if (currentItem == 0) {
            all.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        } else if (currentItem == 1) {
            dealing.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        } else if (currentItem == 2) {
            success.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        } else if (currentItem == 3) {
            defeat.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        }else if (currentItem == 4) {
            fail.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        }
    }

    public void setImmerseLayout(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int statusBarHeight = getStatusBarHeight(this.getBaseContext());
            view.setPadding(0, statusBarHeight, 0, 0);

        }
    }
    /**
     * 用于获取状态栏的高度。 使用Resource对象获取
     *
     * @return 返回状态栏高度的像素值。
     */
    private int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
